﻿  var contracts,existAccounts;
  var feb,seb,sebn;
  function getE(){ 
        var PublicKey = "CF87D7B4C864F4842F1D337491A48FFF54B73A17300E8E42FA365420393AC0346AE55D8AFAD975DFA175FAF0106CBA81AF1DDE4ACEC284DAC6ED9A0D8FEB1CC070733C58213EFFED46529C54CEA06D774E3CC7E073346AEBD6C66FC973F299EB74738E400B22B1E7CDC54E71AED059D228DFEB5B29C530FF341502AE56DDCFE9";
		var RSA = new RSAKey();			
		RSA.setPublic(PublicKey, "10001");	
		var pp = $('#qq #pwd').val();
		var vc = $('#qq #verifyCode').val();	
		var ts = $('#qq #ts').val();		
		var ev =pp+ '\n' + ts + '\n';					
		var Res = RSA.encrypt(ev);			
		if (Res)
		{		   
			$("#qq #p").val(hex2b64(Res));								
		} 
    }
   function getVerifyImg(){
       var img = $('#qq #verifyCodeImg');
       $.get('GetNewQQVerifyCodeImg','',function(res){var data = eval("("+res+")");img.attr('src',data.ImageUrl);var cookies = $('#qq #cookies'); cookies.val(data.CookieString);});
    }
   $(document).ready(function(){  
       feb = $('#findExaxtBtn');
       seb =$('#SubmitExactBtn');
       sebn = $('#SubmitExactByName');
       feb.bind('click',function(){$('#result').html('');var container = $('#findExact'); if(container.css('display')=='none'){container.fadeIn('slow');} else{container.fadeOut('slow');}});
       seb.bind('click',findByEmail);
       sebn.bind('click',findByName);
       $('#inviteEmail').bind('click',function(){$('#result').html('');var container = $('#email'); if(container.css('display')=='none'){container.fadeIn('slow');} else{container.fadeOut('slow');}});
       $("#mailform").submit(function(){
           var f = $("#mailform");      
           var action = f.attr("action");  
           var uid = $('#mailform #uid').val();            
           var pwd = $('#mailform #pwd').val(); 
           var emailSystem = $('#mailform #EmailSystems option[selected]').text();           
           var postData = "uid="+uid+"&pwd="+pwd+"&EmailSystems="+emailSystem;
           $.ajax({
           type:'POST',
           url:action,
           data:postData,
           success:function(res){    
                   if(res==''){
                       $('#result').html('输入信息错误或者该帐号下没有联系人！');
                       return false;  
                   }
                   var data = eval("("+res+")");
                   if(data.length==0){
                     $('#result').html('没有联系人！');
                     return false;  
                   } 
                   contracts = data.ContractList;
                   existAccounts = data.ExistAccounts;
                   if(existAccounts.length>0)
                   {
                       $('#result').html('在您'+$('#EmailSystems option[selected]').text()+'的邮箱中找到'+existAccounts.length+'个联系人:');           
                       $("#addForm").show('slow');
                       $('#result').show();
                       $.each(existAccounts,function(i){var c =existAccounts[i]; $('#existAccountList').append("<tr><td width='80px'><input type='checkbox' id='chooseMe'/></td><td>"+c.TrueName+"</td><td>"+c.Email+"</td></tr>");})  
                   }
                   else if(contracts.length>0){
                       sendInvite();
                   }
                   else{
                       $('#result').html('在您'+$('#EmailSystems option[selected]').text()+'的邮箱中找到'+existAccounts.length+'个联系人:');
                       $('#result').show();           
                   }
               },  
           error:function(req, textStatus, errorThrown){
                   $('#result').html('输入信息错误导致系统执行异常，请重新输入数据！');
                   $('#result').show();
                    return false;  
            }
           });
           showDialog();
           return false;
       });     
       
       $('#inviteQQ').bind('click',function(){$('#result').html('');var container = $('#qq'); if(container.css('display')=='none'){container.fadeIn('slow');} else{container.fadeOut('slow');}});
       $('#qq #uid').bind('blur',function(){var img = $('#qq #verifyCodeImg');if(img.attr('src')){return;} img.attr('src','/Content/Images/loading.gif');$('#qq #verifyCodeContainer').show();getVerifyImg();});
       
       $('#qqForm').submit(function(){          
          var f =  $('#qqForm') ;
          var uid = $('#qq #uid');
          var postData =f.serialize();
          var action = f.attr('action');
          $.ajax({
            type:'POST',
            url:action,
            data:postData,
            success:function(res){
                if(res==''){
                   $('#result').html('输入信息错误或者该帐号下没有联系人！');
                   $('#result').show();
                   return false;  
                }
                var data = eval("("+res+")");
                contracts = data.ContractList;
                existAccounts = data.ExistAccounts;
                if(data.length==0){
                    $('#result').html('输入信息错误或者该帐号下没有联系人！');
                    $('#result').show();
                    return false;  
                }    
                if(existAccounts.length>0){
                    $('#result').html('您在QQ:'+uid.val()+'上的联系人如下:');    
                    $("#addForm").show('slow');         
                    $.each(existAccounts,function(i){
                         var c = existAccounts[i];
                         $('#qq #existAccountList').append("<tr><td width='80px'><input type='checkbox' id='chooseMe'/></td><td>"+c.Name+"</td><td>"+c.Email+"</td></tr>");
                     });     
                    $('#result').show(); 
                }      
                else{
                    sendInvite();
                }
              },
              error:function(){
                    $('#result').html('输入信息错误导致系统执行异常，请重新输入数据！');
                    $('#result').show();
                    return false;  
              }                 
          });    
          showDialog();
          return false;
       });       
       
       $('#inviteLive').bind('click',function(){$('#result').html('');var container = $('#live'); if(container.css('display')=='none'){container.fadeIn('slow');} else{container.fadeOut('slow');container.hide();}});
       $('#doInviteLive').bind('click',function(){getLiveContracts();});
       jQuery().ajaxStart(function(){
          $('#loadImg').show();
       });    
       jQuery().ajaxComplete(function(){
          $('#loadImg').hide();
       });   
       $('#addForm').submit(function(){
          var f = $("#addForm");      
          var action = f.attr("action");  
          var checkBoxs = $('#existAccountList input[@type=checkbox]');  
          var data = new Array();         
          $.each(checkBoxs,function(i){
              var cb = checkBoxs[i];
              if(cb.checked){    
                 data.push(existAccounts[i]);
              }               
          });          
          if(data.length==0){
              alert("至少选择一位联系人添加为好友！");
              return false;
          }
          var groupID =$('#addForm #MyFriendGroup option[@selected]').val();      
          var dataJson ='postData='+toJSON(data)+'&groupID='+groupID;     
          $.post(action,dataJson,function(res){
              if(contracts.length>0){              
                 sendInvite();          
              }  
              else{
                alert("添加好友成功！");
                $('#dialogContent').dialog('close');
              }     
          });     
          return false;
       });  
       $('#sendForm').submit(function(){
          var f = $("#sendForm");      
          var action = f.attr("action");  
          var checkBoxs = $('#contractList input[@type=checkbox]');  
          var data = new Array();         
          $.each(checkBoxs,function(i){
              var cb = checkBoxs[i];
              if(cb.checked){    
                 data.push(contracts[i]);
              }               
          });          
          var dataJson ='postData='+toJSON(data);          
          $.post(action,dataJson,function(res){
             alert(res);                      
          });     
          return false;
       }); 
    })       
    
    function findByEmail(){      
          var exactEmail = $('#exactEmail').val();
          if(exactEmail==''){
              alert('必须输入邮箱地址！');
              return false;
          }
           var postData = "exactEmail=" +exactEmail ;
           $.ajax({
           type:'POST',
           url:'FindByEmail',
           data:postData,
           success:function(res){    
                   if(res==''){
                       $('#result').html('输入信息错误或者该帐号下没有联系人！');
                       return false;  
                   }
                   var data = eval("("+res+")");
                   if(data.length==0){
                     $('#result').html('没有联系人！');
                     return false;  
                   } 
                   contracts = data.ContractList;
                   existAccounts = data.ExistAccounts;
                   if(existAccounts.length>0){                 
                       $('#result').html('找到'+existAccounts.length+'个可加为好友的联系人:');           
                       $("#addForm").show('slow');
                       $('#result').show();
                       $('#Skip').hide();
                       $.each(existAccounts,function(i){var c =existAccounts[i]; $('#existAccountList').append("<tr><td width='80px'><input type='checkbox' id='chooseMe'/></td><td>"+c.TrueName+"</td><td>"+c.Email+"</td></tr>");})   
                   }
                   else{
                       $('#result').html('找到'+existAccounts.length+'个可加为好友的联系人:'); 
                       $('#result').show();                       
                   }
              },  
           error:function(req, textStatus, errorThrown){
                   $('#result').html('输入信息错误导致系统执行异常，请重新输入数据！');
                   $('#result').show();
                   return false;  
            }
           });
           showDialog();
           return false;
    }
    function findByName(){      
          var trueName = $('#trueName').val();
          if(trueName==''){
              alert('必须输入真实姓名！');
              return false;
          }
           var postData = "trueName=" +trueName ;
           $.ajax({
           type:'POST',
           url:'FindByName',
           data:postData,
           success:function(res){    
                   if(res==''){
                       $('#result').html('输入信息错误或者该帐号下没有联系人！');
                       return false;  
                   }
                   var data = eval("("+res+")");
                   if(data.length==0){
                     $('#result').html('没有联系人！');
                     return false;  
                   } 
                   contracts = data.ContractList;
                   existAccounts = data.ExistAccounts;
                   if(existAccounts.length>0){                 
                       $('#result').html('找到'+existAccounts.length+'个可加为好友的联系人:');           
                       $("#addForm").show('slow');
                       $('#result').show();
                       $('#Skip').hide();
                       $.each(existAccounts,function(i){var c =existAccounts[i]; $('#existAccountList').append("<tr><td width='80px'><input type='checkbox' id='chooseMe'/></td><td>"+c.TrueName+"</td><td>"+c.Email+"</td></tr>");})   
                   }
                   else{
                       $('#result').html('找到'+existAccounts.length+'个可加为好友的联系人:'); 
                       $('#result').show();                       
                   }
              },  
           error:function(req, textStatus, errorThrown){
                   $('#result').html('输入信息错误导致系统执行异常，请重新输入数据！');
                   $('#result').show();
                   return false;  
            }
           });
           showDialog();
           return false;
    }
    
    function sendInvite(){          
        $('#addForm').fadeOut('slow');
        $('#result').html('从下面的联系人列表中选择邀请对象:');
        $('#result').show();
        $.each(contracts,function(i){var c =contracts[i]; $('#contractList').append("<tr><td width='80px'><input type='checkbox' id='chooseMe'/></td><td>"+c.Name+"</td><td>"+c.Email+"</td></tr>");});
        $('#sendForm').fadeIn('slow');   
    }
    
    function getLiveContracts(){    
       $.ajax(
       {
         type:'GET',
         url:'GetLiveContracts',
         dataType:'json',
         success:function(res){
              if(res==''){
                   $('#result').html('输入信息错误或者该帐号下没有联系人！');
                   $('#result').show();
                   return false;  
               }
               var data = eval(res);
               if(data.length==0){
                    $('#result').html('没有联系人！');
                    $('#result').show();
                    return false;  
               } 
               contracts = data.ContractList;
               existAccounts = data.ExistAccounts;
               if(existAccounts.length>0){
                   $('#result').html('您在 Live Messager上的联系人如下:');
                   $("#addForm").show('slow');
                   $.each(existAccounts,function(i){
                       var c =existAccounts[i];$('#existAccountList').append("<tr><td width='80px'><input type='checkbox' id='chooseMe'/></td><td>"+c.TrueName+"</td><td>"+c.Email+"</td></tr>");
                   });        
                  $('#result').show();
              }
              else if(contracts.length>0){
                   sendInvite();
              }
              else{
                   $('#result').html('您在 Live Messager上的联系人如下:');
                   $('#result').show();
              }
             },
            error:function(){
                    $('#result').html('输入信息错误导致系统执行异常，请重新输入数据！');
                    $('#result').show();
                    return false;  
              }            
      }); 
      showDialog();
    }    
    
    function showDialog(){
       $('#dialogContent').dialog({autoOpen:false,modal: true, width:650,height:450,title:'查找好友/邀请好友', overlay: { opacity: 0.5, background: "black" },close:reload});     
       $('#dialogContent').dialog('open');
       $('#dialogContent').show();       
    }
        
    function reload(){    
        $('#result').html('');
        $('#existAccountList tr:not(tr:first)').remove();
        $('#addForm').hide();
        $('#contractList tr:not(tr:first)').remove();
         $('#sendForm').hide();
         $('#dialogContent').hide();
         $('#result').hide();
    }
 
    function chooseAll(tableID){
      var header = $('#chooseHeader');
      var checkBoxs = $('#'+tableID+' input[@type=checkbox]');  
      if(header.text()=='全选'){         
          $.each(checkBoxs,function(i){
              var cb = checkBoxs[i];
              cb.checked = true;          
          });
          header.text('反选');
      }
      else{             
          $.each(checkBoxs,function(i){
              var cb = checkBoxs[i];
              cb.checked = false;          
          });
          header.text('全选');
      }
    }